require "proto/areaarena/message/ReqAreaArenaBattleMessage" -- 挑战请求
require "proto/areaarena/message/ReqAreaArenaSetDefFormationMessage" -- 设置防守队伍请求
require "proto/areaarena/message/ReqAreaArenaDrawArenaBoxMessage" -- 领取跨服竞技场宝箱请求
require "proto/areaarena/message/ReqAreaArenaBuyTicketMessage"   -- 购买挑战券请求
require "proto/areaarena/message/ReqAreaArenaAddSharedRecordMessage" -- 跨服竞技场增加分享记录请求
require "proto/areaarena/message/ReqAreaArenaBattleRecordsMessage" -- 跨服竞技场战斗记录请求
require "proto/areaarena/message/ReqAreaArenaGetRanksMessage" -- 跨服竞技场排行请求
require "proto/areaarena/message/ReqAreaArenaOpponentMessage" -- 对手列表请求
require "proto/areaarena/message/ReqAreaArenaSharedRecordsMessage" -- 跨服竞技场分享记录请求
require "proto/areaarena/message/ReqAreaArenaInfoMessage" -- 跨服竞技场详情请求

crossArenaHandler = crossArenaHandler or {};
G.serverData = G.serverData or {};

-- 跨服竞技场详情返回
function crossArenaHandler.receiveCrossArenaInfo(msg) 
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossBaseInfo(msg)
        FireEvent(Config.EventType.Fresh_Cross_Arena)
    end
end

--- @param msg ResArenaBattleResultMessage 跨服挑战结果返回
function crossArenaHandler.receiveCrossBattleResult(msg)
    -- print("crossArenaHandler.receiveCrossBattleResult(msg)------------------------->", table.tostring(msg) )
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end
    logicMgr.ArenaManager.refreshCrossBattleInfo(msg)
    FightManager.InitRewards(msg.items)
    BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)
end

-- 设置跨服防守队伍返回
function crossArenaHandler.receiveCrossDefFormation(msg) 
    -- print(" crossArenaHandler.receiveCrossDefFormation(msg) --------------------------->", table.tostring(msg) )
    if msg.state == 0 then
        local formations = {};
        for i = 1, #msg.formation do
            table.insert(formations, tostring(msg.formation[i]))
        end
        -- print("crossArenaHandler.receiveCrossDefFormation--------------------->formations:", table.tostring(formations) )
        logicMgr.FormationManager.SetFormation(EnumConst.FormationType.CrossArenaDef, formations)
        FireEvent(Config.EventType.Fresh_CrossArenaFormation)
    end
end

-- 领取跨服竞技场宝箱返回
function crossArenaHandler.receiveCrossDrawArenaBox(msg) 
    -- print("crossArenaHandler.receiveCrossDrawArenaBox------------------------------>", table.tostring(msg) )
    if msg.state == 0 then
        logicMgr.ArenaManager.addCrossRewardBox(msg.boxId)
        local rewardsCfg = decodeJsonStrToTabel(G.dataTable["t_arenaboxrewardcs"][tostring(msg.boxId)].f_rewards);
        local rewards = {};
        for k, v in pairs(rewardsCfg) do
            local item = {};
            item.num = v[2];
            item.itemModelId = v[1]
            table.insert(rewards, item)
        end
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards)
        FireEvent(Config.EventType.Fresh_Cross_Arena)
    end
end

-- 购买跨服挑战券返回
function crossArenaHandler.receiveCrossBuyTicket(msg)
    -- print("crossArenaHandler.receiveCrossBuyTicket(msg)---------------------------->", table.tostring(msg) )
    if msg.state == 0 then
        logicMgr.ArenaManager.addBuyCrossTicketCount(msg.num)
        FireEvent(Config.EventType.Fresh_CrossArenaTicket)
    end
end

--- 跨服竞技场增加分享记录请求返回
function crossArenaHandler.receiveCrossAddSharedRecord(msg)
    -- print("crossArenaHandler.receiveCrossAddSharedRecord--------------------->", table.tostring(msg) )
    if msg.state == 0 then
        logicMgr.ArenaManager.onAddCrossSharedSuccess(msg.fightId)
    end
end

--- 跨服竞技场战斗记录请求返回
function crossArenaHandler.receiveCrossArenaBattleRecords(msg)
    -- print("receiveCrossArenaBattleRecords-------------->", table.tostring(msg) )
    if msg.state == 0 then
        local records =  msg.records
        for k, v in ipairs(msg.serverIds) do
            records[k].serverId = v
        end
        logicMgr.ArenaManager.setCrossRecordInfo(msg.recordVersion, records)
        FireEvent(Config.EventType.Fresh_CrossArenaRecords)
    end
end

--- 跨服竞技场对手列表返回
function crossArenaHandler.receiveCrossOpponentInfo(msg)
    -- print("receiveCrossOpponentInfo-------------->", table.tostring(msg) )
    if msg.state == 0 then
        ArenaManager.setCrossOpponentInfo(msg)
        FireEvent(Config.EventType.Fresh_Cross_Arena)
    end
end

--- 跨服竞技场分享记录请求返回
function crossArenaHandler.receiveCrossArenaSharedRecords(msg)
    -- print("receiveCrossArenaSharedRecords-------------->", table.tostring(msg) )
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossSharedRecords(msg)
        FireEvent(Config.EventType.Fresh_CrossArenaSharedRecords)
    end
end

--  跨服竞技场排行请求返回
function crossArenaHandler.receiveCrossArenaGetRanks(msg)
    -- print("crossArenaHandler.receiveCrossArenaGetRanks(msg)------>", table.tostring(msg) )
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossRankInfo(msg)
        FireEvent(Config.EventType.Fresh_CrossArenaRanks)
    end
end

-- 
function crossArenaHandler.sendCrossArenaBattle(foeId, isAi, formation, fightId)
    -- print("crossArenaHandler.sendCrossArenaBattle()--------------------------->", foeId, isAi, formation, fightId)
    local req = ReqAreaArenaBattleMessage.New()
    req.foeId = foeId
    req.isAi = isAi
    req.formation = formation
    req.version = Config.ProtoVersion
    if fightId ~= nil then
        req.fightId = fightId
    else
        req.fightId = 0
    end
    sendReqMessage(req)
end

-- 发送跨服防守阵容
function crossArenaHandler.sendCrossDefFormation(formation)
    -- print("crossArenaHandler.sendCrossDefFormation()--------------------------->", table.tostring(formation) )
    local req = ReqAreaArenaSetDefFormationMessage.New()
    req.formation = formation
    sendReqMessage(req)
end

-- 领取跨服竞技场宝箱请求
function crossArenaHandler.sendCrossDrawArenaBox(boxId)
    -- print("crossArenaHandler.sendCrossDrawArenaBox()--------------------------->", boxId)
    local req = ReqAreaArenaDrawArenaBoxMessage.New()
    req.boxId = boxId
    sendReqMessage(req)
end

-- 
function crossArenaHandler.sendCrossBuyTicket(count)
    -- print("crossArenaHandler.sendCrossBuyTicket()--------------------------->", count)
    local req = ReqAreaArenaBuyTicketMessage.New()
    req.num = count
    sendReqMessage(req)
end

--- 发送跨服竞技场记录请求
--- @param localRecordVer number 本地版本号
function crossArenaHandler.sendCrossArenaBattleRecords(localRecordVer)
    -- print("crossArenaHandler.sendCrossArenaBattleRecords()--------------------------->", localRecordVer)
    local req = ReqAreaArenaBattleRecordsMessage.New()
    req.recordVersion = localRecordVer
    sendReqMessage(req)
end

--- 发送跨服挑战列表请求
function crossArenaHandler.sendCrossArenaOpponentMessage()
    -- print("crossArenaHandler.sendCrossArenaOpponentMessage()--------------------------->")
    local req = ReqAreaArenaOpponentMessage.New()
    sendReqMessage(req)
end

--- 发送跨服分享战报列表请求
function crossArenaHandler.sendCrossArenaSharedRecords()
    -- print("crossArenaHandler.sendCrossArenaSharedRecords()--------------------------->")
    local req = ReqAreaArenaSharedRecordsMessage.New()
    sendReqMessage(req)
end

--- 发送跨服竞技场排行榜请求
--- @param rankVersion number 排行榜版本号
function crossArenaHandler.sendCrossArenaGetRanks(rankVersion)
    -- print("crossArenaHandler.sendCrossArenaGetRanks(rankVersion)------------------", rankVersion)
    local req = ReqAreaArenaGetRanksMessage.New()
    req.rankVersion = rankVersion
    sendReqMessage(req)
end

--发送跨服竞技场信息请求
function crossArenaHandler.sendReqCrossArenaInfo()
    -- print("crossArenaHandler.sendReqCrossArenaInfo---------------------------->")
    local req = ReqAreaArenaInfoMessage.New()
    sendReqMessage(req)
end

function crossArenaHandler.sendCrossAddSpecialBattle(recordId)
    local request = ReqAreaArenaAddSharedRecordMessage.New();
    request.fightId = recordId;
    sendReqMessage(request);
end
